// pages/recruit/personal/resume/resume.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: {

    },
    isInfoTrue: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function(options) {
    const that = this;
    getApp().globalData.get("/api/resume",{}).then(res=>{
      if (res.code == 200){
        that.data.info = res.data
        getApp().globalData.resumeId = res.data.resume.id;
        let age = that.getAge(res.data.resume.birthday)
        that.data.info.resume.age = age
        that.setData({
          info: that.data.info,
          isInfoTrue: true
        })
      } else if (res.code == 2002){
        that.setData({
          isInfoTrue: false
        })
        wx.showToast({
          title: '您还没有简历，请添加',
          icon: "none"
        })
      }
    })
  },
  changeedu: function (e) {
    let index = e.currentTarget.dataset.index;
    let obj = this.data.info.educationals[index];
    wx.navigateTo({
      url: '/pages/recruit/personal/resume/addeducation/addeducation',
      success: function(res) {
        res.eventChannel.emit('crossdata', { data: obj })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  changework: function (e) {
    let index = e.currentTarget.dataset.index;
    let obj = this.data.info.workExperiences[index];
    wx.navigateTo({
      url: '/pages/recruit/personal/resume/addworkex/addworkex',
      success: function (res) {
        res.eventChannel.emit('crossdata', { data: obj })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  changeskill: function (e){
    let index = e.currentTarget.dataset.index;
    let obj = this.data.info.certificates[index];
    wx.navigateTo({
      url: '/pages/recruit/personal/resume/addskill/addskill',
      success: function (res) {
        res.eventChannel.emit('crossdata', { data: obj })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  addselfappraisal: function(e) {
    wx.navigateTo({
      url: '/pages/recruit/personal/resume/addselfappraisal/addselfappraisal',
      success: function (res) {
        res.eventChannel.emit('crossdata', { data: e.currentTarget.dataset.value })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  getAge: function (strBirthday){
    var returnAge,
      strBirthdayArr = strBirthday.split("-"),
      birthYear = strBirthdayArr[0],
      birthMonth = strBirthdayArr[1],
      birthDay = strBirthdayArr[2],
      d = new Date(),
      nowYear = d.getFullYear(),
      nowMonth = d.getMonth() + 1,
      nowDay = d.getDate();
    if (nowYear == birthYear) {
      returnAge = 0;//同年 则为0周岁
    }
    else {
      var ageDiff = nowYear - birthYear; //年之差
      if (ageDiff > 0) {
        if (nowMonth == birthMonth) {
          var dayDiff = nowDay - birthDay;//日之差
          if (dayDiff < 0) {
            returnAge = ageDiff - 1;
          } else {
            returnAge = ageDiff;
          }
        } else {
          var monthDiff = nowMonth - birthMonth;//月之差
          if (monthDiff < 0) {
            returnAge = ageDiff - 1;
          }
          else {
            returnAge = ageDiff;
          }
        }
      } else {
        returnAge = -1;//返回-1 表示出生日期输入错误 晚于今天
      }
    }
    return returnAge;//返回周岁年龄
  }
})